<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Grok Processor | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'grok-processor.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/grok-processor.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/grok-processor.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/grok-processor.html" rel="nofollow" target="_blank">../en/grok-processor.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="ingest.html">Ingest node</a></span>
»
<span class="breadcrumb-link"><a href="ingest-processors.html">Processors</a></span>
»
<span class="breadcrumb-node">Grok Processor</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geoip-processor.html">« GeoIP Processor</a>
</span>
<span class="next">
<a href="gsub-processor.html">Gsub Processor »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="grok-processor"></a>Grok Processor<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h2>
</div></div></div>
<p>Extracts structured fields out of a single text field within a document. You choose which field to
extract matched fields from, as well as the grok pattern you expect will match. A grok pattern is like a regular
expression that supports aliased expressions that can be reused.</p>
<p>This tool is perfect for syslog logs, apache and other webserver logs, mysql logs, and in general, any log format
that is generally written for humans and not computer consumption.
This processor comes packaged with many
<a href="https://github.com/elastic/elasticsearch/blob/7.7/libs/grok/src/main/resources/patterns" class="ulink" target="_top">reusable patterns</a>.</p>
<p>If you need help building patterns to match your logs, you will find the <a href="https://www.elastic.co/guide/en/kibana/7.7/xpack-grokdebugger.html" class="ulink" target="_top">Grok Debugger</a> tool quite useful! The Grok Debugger is an X-Pack feature under the Basic License and is therefore <span class="strong strong"><strong>free to use</strong></span>. The Grok Constructor at <a href="http://grokconstructor.appspot.com/" class="ulink" target="_top">http://grokconstructor.appspot.com/</a> is also a useful tool.</p>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="grok-basics"></a>Grok Basics<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Grok sits on top of regular expressions, so any regular expressions are valid in grok as well.
The regular expression library is Oniguruma, and you can see the full supported regexp syntax
<a href="https://github.com/kkos/oniguruma/blob/master/doc/RE" class="ulink" target="_top">on the Oniguruma site</a>.</p>
<p>Grok works by leveraging this regular expression language to allow naming existing patterns and combining them into more
complex patterns that match your fields.</p>
<p>The syntax for reusing a grok pattern comes in three forms: <code class="literal">%{SYNTAX:SEMANTIC}</code>, <code class="literal">%{SYNTAX}</code>, <code class="literal">%{SYNTAX:SEMANTIC:TYPE}</code>.</p>
<p>The <code class="literal">SYNTAX</code> is the name of the pattern that will match your text. For example, <code class="literal">3.44</code> will be matched by the <code class="literal">NUMBER</code>
pattern and <code class="literal">55.3.244.1</code> will be matched by the <code class="literal">IP</code> pattern. The syntax is how you match. <code class="literal">NUMBER</code> and <code class="literal">IP</code> are both
patterns that are provided within the default patterns set.</p>
<p>The <code class="literal">SEMANTIC</code> is the identifier you give to the piece of text being matched. For example, <code class="literal">3.44</code> could be the
duration of an event, so you could call it simply <code class="literal">duration</code>. Further, a string <code class="literal">55.3.244.1</code> might identify
the <code class="literal">client</code> making a request.</p>
<p>The <code class="literal">TYPE</code> is the type you wish to cast your named field. <code class="literal">int</code>, <code class="literal">long</code>, <code class="literal">double</code>, <code class="literal">float</code> and <code class="literal">boolean</code> are supported types for coercion.</p>
<p>For example, you might want to match the following text:</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">3.44 55.3.244.1</pre>
</div>
<p>You may know that the message in the example is a number followed by an IP address. You can match this text by using the following
Grok expression.</p>
<div class="pre_wrapper lang-txt">
<pre class="programlisting prettyprint lang-txt">%{NUMBER:duration} %{IP:client}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="using-grok"></a>Using the Grok Processor in a Pipeline<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<div class="table">
<a id="grok-options"></a>
<p class="title"><strong>Table 48. Grok Options</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="Grok Options">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
<col class="col_4">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Required</th>
<th align="left" valign="top">Default</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">field</code></p></td>
<td align="left" valign="top"><p>yes</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>The field to use for grok expression parsing</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">patterns</code></p></td>
<td align="left" valign="top"><p>yes</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>An ordered list of grok expression to match and extract named captures with. Returns on the first expression in the list that matches.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">pattern_definitions</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>A map of pattern-name and pattern tuples defining custom patterns to be used by the current processor. Patterns matching existing names will override the pre-existing definition.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">trace_match</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>false</p></td>
<td align="left" valign="top"><p>when true, <code class="literal">_ingest._grok_match_index</code> will be inserted into your matched document’s metadata with the index into the pattern found in <code class="literal">patterns</code> that matched.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ignore_missing</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>false</p></td>
<td align="left" valign="top"><p>If <code class="literal">true</code> and <code class="literal">field</code> does not exist or is <code class="literal">null</code>, the processor quietly exits without modifying the document</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">if</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>Conditionally execute this processor.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">on_failure</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>Handle failures for this processor. See <a class="xref" href="handling-failure-in-pipelines.html" title="Handling Failures in Pipelines"><em>Handling Failures in Pipelines</em></a>.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ignore_failure</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p><code class="literal">false</code></p></td>
<td align="left" valign="top"><p>Ignore failures for this processor. See <a class="xref" href="handling-failure-in-pipelines.html" title="Handling Failures in Pipelines"><em>Handling Failures in Pipelines</em></a>.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">tag</code></p></td>
<td align="left" valign="top"><p>no</p></td>
<td align="left" valign="top"><p>-</p></td>
<td align="left" valign="top"><p>An identifier for this processor. Useful for debugging and metrics.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Here is an example of using the provided patterns to extract out and name structured fields from a string field in
a document.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "description" : "...",
    "processors": [
      {
        "grok": {
          "field": "message",
          "patterns": ["%{IP:client} %{WORD:method} %{URIPATHPARAM:request} %{NUMBER:bytes:int} %{NUMBER:duration:double}"]
        }
      }
    ]
  },
  "docs":[
    {
      "_source": {
        "message": "55.3.244.1 GET /index.html 15824 0.043"
      }
    }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1091.console"></div>
<p>This pipeline will insert these named captures as new fields within the document, like so:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_type": "_doc",
        "_id": "_id",
        "_source" : {
          "duration" : 0.043,
          "request" : "/index.html",
          "method" : "GET",
          "bytes" : 15824,
          "client" : "55.3.244.1",
          "message" : "55.3.244.1 GET /index.html 15824 0.043"
        },
        "_ingest": {
          "timestamp": "2016-11-08T19:43:03.850+0000"
        }
      }
    }
  ]
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="custom-patterns"></a>Custom Patterns<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The Grok processor comes pre-packaged with a base set of patterns. These patterns may not always have
what you are looking for. Patterns have a very basic format. Each entry has a name and the pattern itself.</p>
<p>You can add your own patterns to a processor definition under the <code class="literal">pattern_definitions</code> option.
Here is an example of a pipeline specifying custom pattern definitions:</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "description" : "...",
  "processors": [
    {
      "grok": {
        "field": "message",
        "patterns": ["my %{FAVORITE_DOG:dog} is colored %{RGB:color}"],
        "pattern_definitions" : {
          "FAVORITE_DOG" : "beagle",
          "RGB" : "RED|GREEN|BLUE"
        }
      }
    }
  ]
}</pre>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="trace-match"></a>Providing Multiple Match Patterns<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Sometimes one pattern is not enough to capture the potential structure of a field. Let’s assume we
want to match all messages that contain your favorite pet breeds of either cats or dogs. One way to accomplish
this is to provide two distinct patterns that can be matched, instead of one really complicated expression capturing
the same <code class="literal">or</code> behavior.</p>
<p>Here is an example of such a configuration executed against the simulate API:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST _ingest/pipeline/_simulate
{
  "pipeline": {
  "description" : "parse multiple patterns",
  "processors": [
    {
      "grok": {
        "field": "message",
        "patterns": ["%{FAVORITE_DOG:pet}", "%{FAVORITE_CAT:pet}"],
        "pattern_definitions" : {
          "FAVORITE_DOG" : "beagle",
          "FAVORITE_CAT" : "burmese"
        }
      }
    }
  ]
},
"docs":[
  {
    "_source": {
      "message": "I love burmese cats!"
    }
  }
  ]
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1092.console"></div>
<p>response:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "docs": [
    {
      "doc": {
        "_type": "_doc",
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "message": "I love burmese cats!",
          "pet": "burmese"
        },
        "_ingest": {
          "timestamp": "2016-11-08T19:43:03.850+0000"
        }
      }
    }
  ]
}</pre>
</div>
<p>Both patterns will set the field <code class="literal">pet</code> with the appropriate match, but what if we want to trace which of our
patterns matched and populated our fields? We can do this with the <code class="literal">trace_match</code> parameter. Here is the output of
that same pipeline, but with <code class="literal">"trace_match": true</code> configured:</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
  "docs": [
    {
      "doc": {
        "_type": "_doc",
        "_index": "_index",
        "_id": "_id",
        "_source": {
          "message": "I love burmese cats!",
          "pet": "burmese"
        },
        "_ingest": {
          "_grok_match_index": "1",
          "timestamp": "2016-11-08T19:43:03.850+0000"
        }
      }
    }
  ]
}</pre>
</div>
<p>In the above response, you can see that the index of the pattern that matched was <code class="literal">"1"</code>. This is to say that it was the
second (index starts at zero) pattern in <code class="literal">patterns</code> to match.</p>
<p>This trace metadata enables debugging which of the patterns matched. This information is stored in the ingest
metadata and will not be indexed.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="grok-processor-rest-get"></a>Retrieving patterns from REST endpoint<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>The Grok Processor comes packaged with its own REST endpoint for retrieving which patterns the processor is packaged with.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">GET _ingest/processor/grok</pre>
</div>
<div class="console_widget" data-snippet="snippets/1093.console"></div>
<p>The above request will return a response body containing a key-value representation of the built-in patterns dictionary.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "patterns" : {
    "BACULA_CAPACITY" : "%{INT}{1,3}(,%{INT}{3})*",
    "PATH" : "(?:%{UNIXPATH}|%{WINPATH})",
    ...
}</pre>
</div>
<p>This can be useful to reference as the built-in patterns change across versions.</p>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="grok-watchdog"></a>Grok watchdog<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>Grok expressions that take too long to execute are interrupted and
the grok processor then fails with an exception. The grok
processor has a watchdog thread that determines when evaluation of
a grok expression takes too long and is controlled by the following
settings:</p>
<div class="table">
<a id="grok-watchdog-options"></a>
<p class="title"><strong>Table 49. Grok watchdog settings</strong></p>
<div class="table-contents">
<table border="1" cellpadding="4px" summary="Grok watchdog settings">
<colgroup>
<col class="col_1">
<col class="col_2">
<col class="col_3">
</colgroup>
<thead>
<tr>
<th align="left" valign="top">Name</th>
<th align="left" valign="top">Default</th>
<th align="left" valign="top">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left" valign="top"><p><code class="literal">ingest.grok.watchdog.interval</code></p></td>
<td align="left" valign="top"><p>1s</p></td>
<td align="left" valign="top"><p>How often to check whether there are grok evaluations that take longer than the maximum allowed execution time.</p></td>
</tr>
<tr>
<td align="left" valign="top"><p><code class="literal">ingest.grok.watchdog.max_execution_time</code></p></td>
<td align="left" valign="top"><p>1s</p></td>
<td align="left" valign="top"><p>The maximum allowed execution of a grok expression evaluation.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="grok-debugging"></a>Grok debugging<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/ingest/processors/grok.asciidoc">edit</a>
</h3>
</div></div></div>
<p>It is advised to use the <a href="https://www.elastic.co/guide/en/kibana/7.7/xpack-grokdebugger.html" class="ulink" target="_top">Grok Debugger</a> to debug grok patterns. From there you can test one or more
patterns in the UI against sample data. Under the covers it uses the same engine as ingest node processor.</p>
<p>Additionally, it is recommended to enable debug logging for Grok so that any additional messages may also be seen in the Elasticsearch
server log.</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">PUT _cluster/settings
{
  "transient": {
    "logger.org.elasticsearch.ingest.common.GrokProcessor": "debug"
  }
}</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="geoip-processor.html">« GeoIP Processor</a>
</span>
<span class="next">
<a href="gsub-processor.html">Gsub Processor »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>